//
//  RegistViewController.m
//  YYZB
//
//  Created by BY_R on 16/3/25.
//
//

#import "RegistViewController.h"
#import "UserWebService.h"
#import "NSString+MD5.h"
#import "LogViewController.h"
#import "NSString+MobileNo.h"
#import "LoadingView.h"
#import "Button.h"
#import "UserInfoController.h"
#import "ServerViewController.h"

#define TotalTimerCount 60
@interface RegistViewController ()
@property (nonatomic, strong) UserWebService *userWebService;
@property(nonatomic,copy)NSString * Flag;
@property(nonatomic,copy)NSString * numId;
@property (strong, nonatomic) NSTimer *timer;
@property (nonatomic) NSUInteger timerCount;
@property (weak, nonatomic) IBOutlet Button *getVerifyCode;
@property (weak, nonatomic) IBOutlet UIButton *termsButton;
@end

@implementation RegistViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.userWebService = UserWebService.new;
    self.termsButton.selected = YES;
}

- (IBAction)getNum:(id)sender {
    if ([self.phoneTF.text isMobileNumber]) {
        [LoadingView show];
        [self.userWebService getVerifyCode:self.phoneTF.text flag:@"1" completionHanlder:^(NSArray *items, NSError *error) {
            
            [LoadingView dismiss];
            self.numId = [items valueForKey:@"VerifyCodeId"];
            if (error) {
                [LoadingView showToast:[error localizedDescription]];
                
            } else {
            dispatch_async(dispatch_get_main_queue(), ^{

                self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerTicked:) userInfo:nil repeats:YES];
                self.timerCount = TotalTimerCount;
            });
            }
            
        }];
    }
    else{

        [LoadingView showToast:@"手机号有误"];
    }
    
}

- (IBAction)termsCheckButtonTapped:(UIButton *)button {
    button.selected = !button.selected;
}

- (IBAction)termsButtonTapped:(UIButton *)button {
    ServerViewController * vc = [[ServerViewController alloc]init];
    vc.hidesBottomBarWhenPushed = YES;
    vc.title = @"会员协议";
    vc.HTMLURL = @"user/agreement.html";
    [self.navigationController pushViewController:vc animated:YES];
}

- (IBAction)regis:(id)sender {
    if ([self.phoneTF.text isMobileNumber] && self.codeTF.text.length!=0 && self.pswTF.text.length>=6 && self.pswTF.text.length<=16 && self.numId.length!=0 && self.termsButton.selected) {
        
        [LoadingView show];
        [self.userWebService registe:self.phoneTF.text VerifyCode:self.codeTF.text password:[self.pswTF.text MD5] VerifyCodeId:self.numId ?:@"" source:@"1" completionHanlder:^(NSArray *items, NSError *error) {
            
            [LoadingView dismiss];
            if (error){
                [LoadingView showToast:[error localizedDescription]];
                if ((long)error.code == -21) {
                    [UserInfoController setUserNo:self.phoneTF.text];
                    dispatch_async(dispatch_get_main_queue(), ^{
                        [self.navigationController popToRootViewControllerAnimated:YES];
                    });
                }
                if ((long)error.code == -2) {
                    [LoadingView showToast:@"验证码不正确"];
                }
                
            }
            else {
                [LoadingView showToast:@"注册成功！"];
                [UserInfoController setUserNo:self.phoneTF.text];
                dispatch_async(dispatch_get_main_queue(), ^{
                    [self.navigationController popToRootViewControllerAnimated:YES];
                });
            }
        }];
        
    }
    else if (self.phoneTF.text.length==0||self.codeTF.text.length==0||self.pswTF.text.length==0){
        [LoadingView showToast:@"注册信息不能为空"];
    }
    else if (!self.termsButton.selected) {
        [LoadingView showToast:@"您需要阅读并同意阅读协议"];
    }
    else{
        [LoadingView showToast:@"验证码不正确"];
    }
    
}

- (void)timerTicked:(NSTimer *)timer {
    dispatch_async(dispatch_get_main_queue(), ^{
        self.timerCount -= 1;
        if (self.timerCount == 0) {
            self.timerCount = TotalTimerCount;
            self.getVerifyCode.enabled = YES;
            self.getVerifyCode.title = @"获取验证码";
            [self.timer invalidate];
            self.timer = nil;
        } else {
            self.getVerifyCode.title = [NSString stringWithFormat:@"%@秒", @(self.timerCount)];
            self.getVerifyCode.enabled = NO;
        }
    });
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
